/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kladara;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.UIManager;
import pomocni.Pomocna;
import pomocni.PomocnaBaza;
import pomocni.Poruka;

/**
 *
 * @author damirl
 */
public class TipsterPregled extends javax.swing.JDialog {

    /**
     * Creates new form TipseriPregled
     */
    boolean odabir;

    public TipsterPregled(Component parent, boolean modal, boolean odabir) {
        super();
        initComponents();
        setLocationRelativeTo(parent);
        postavkeTablica();
        this.odabir = odabir;
        this.getRootPane().setDefaultButton(jButtonOdabir);
        if (this.odabir && Pomocna.idPozicioniranje > 0) {
            Pomocna.pozicioniranje(1, 0, tablica);
        }
        Pomocna.postavkeProzora(this, null);
        setVisible(true);
        setModal(modal);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tablica = new quick.dbtable.DBTable();
        jPanel1 = new javax.swing.JPanel();
        jButtonOdabir = new javax.swing.JButton();
        jButtonUpis = new javax.swing.JButton();
        jButtonIzmjena = new javax.swing.JButton();
        jButtonBrisanje = new javax.swing.JButton();
        jButtonIzlaz = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tipsteri");
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(640, 480));
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setResizable(false);

        tablica.setConnection(Konekcija.konekcija);
        tablica.setSelectSql("select  Id as \"Šifra\",Naziv as \"Naziv\",Igram as \"Igram\" , Napomena as \"Napomena\" from TIPSTER ORDER BY Id");
        tablica.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablicaKeyPressed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jButtonOdabir.setMnemonic('O');
        jButtonOdabir.setText("Odabir");
        jButtonOdabir.setMaximumSize(new java.awt.Dimension(80, 30));
        jButtonOdabir.setMinimumSize(new java.awt.Dimension(80, 30));
        jButtonOdabir.setPreferredSize(new java.awt.Dimension(80, 30));
        jButtonOdabir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOdabirActionPerformed(evt);
            }
        });

        jButtonUpis.setMnemonic('U');
        jButtonUpis.setText("Upis");
        jButtonUpis.setMaximumSize(new java.awt.Dimension(80, 30));
        jButtonUpis.setMinimumSize(new java.awt.Dimension(80, 30));
        jButtonUpis.setPreferredSize(new java.awt.Dimension(80, 30));
        jButtonUpis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpisActionPerformed(evt);
            }
        });

        jButtonIzmjena.setMnemonic('I');
        jButtonIzmjena.setText("Izmjena");
        jButtonIzmjena.setMaximumSize(new java.awt.Dimension(80, 30));
        jButtonIzmjena.setMinimumSize(new java.awt.Dimension(80, 30));
        jButtonIzmjena.setPreferredSize(new java.awt.Dimension(80, 30));
        jButtonIzmjena.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIzmjenaActionPerformed(evt);
            }
        });

        jButtonBrisanje.setMnemonic('B');
        jButtonBrisanje.setText("Brisanje");
        jButtonBrisanje.setMaximumSize(new java.awt.Dimension(80, 30));
        jButtonBrisanje.setMinimumSize(new java.awt.Dimension(80, 30));
        jButtonBrisanje.setPreferredSize(new java.awt.Dimension(80, 30));
        jButtonBrisanje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBrisanjeActionPerformed(evt);
            }
        });

        jButtonIzlaz.setMnemonic('z');
        jButtonIzlaz.setText("Izlaz");
        jButtonIzlaz.setMaximumSize(new java.awt.Dimension(80, 30));
        jButtonIzlaz.setMinimumSize(new java.awt.Dimension(80, 30));
        jButtonIzlaz.setPreferredSize(new java.awt.Dimension(80, 30));
        jButtonIzlaz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIzlazActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jButtonOdabir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonUpis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonIzmjena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonBrisanje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonIzlaz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOdabir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonUpis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonIzmjena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonIzlaz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBrisanje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tablica, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tablica, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void postavkeTablica() {
        try {
            tablica.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
            tablica.setEditable(false);
            tablica.refresh();
            //Šifra
            tablica.getColumn(0).setMaxWidth(60);
            //Naziv
            tablica.getColumn(1).setMaxWidth(150);
            //Igram 
            Pomocna.instalirajCheckBox(tablica, 2);
            tablica.getColumn(2).setMinWidth(80);
            tablica.getColumn(2).setMaxWidth(80);             
            TablicaSkin skin = new TablicaSkin();
            tablica.setSkin(skin);
            Object put = UIManager.put("Table.gridColor", Color.black);
        } catch (SQLException ex) {
            Poruka.greska(this, ex.getMessage());
        }
    }

    ;
    
    private void jButtonIzlazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIzlazActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonIzlazActionPerformed

    private void jButtonUpisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpisActionPerformed
        try {
            // TODO add your handling code here:
            new TipsterAzuriranje(this, true, 1, 0);
            tablica.refresh();
            Pomocna.pozicioniranje(1, 0, tablica);
        } catch (SQLException ex) {
            Poruka.greska(this, ex.getMessage());
        }
    }//GEN-LAST:event_jButtonUpisActionPerformed

    private void jButtonIzmjenaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIzmjenaActionPerformed
        try {
            new TipsterAzuriranje(this, true, 2, Integer.parseInt(tablica.getValueAt(tablica.getSelectedRow(), 0).toString()));
            Pomocna.redPozicioniranje = tablica.getSelectedRow();
            tablica.refresh();
            Pomocna.pozicioniranje(2, 0, tablica);
        } catch (SQLException ex) {
            Poruka.greska(this, ex.getMessage());
        }
    }//GEN-LAST:event_jButtonIzmjenaActionPerformed

    private void jButtonBrisanjeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBrisanjeActionPerformed
        // TODO add your handling code here:
        Pomocna.redPozicioniranje = tablica.getSelectedRow();
        boolean potvrda = Poruka.potvrda(rootPane, "Obrisati tipstera i pripadajući promet ?");
        if (potvrda) {
            try {
                PomocnaBaza.brisanjeTipsteraIPrometa(Integer.parseInt(tablica.getValueAt(tablica.getSelectedRow(), 0).toString()));
                tablica.refresh();
            } catch (SQLException ex) {
                Poruka.greska(this, ex.getMessage());
            }
        }
        Pomocna.pozicioniranje(2, 0, tablica);
    }//GEN-LAST:event_jButtonBrisanjeActionPerformed

    private void jButtonOdabirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOdabirActionPerformed
        // TODO add your handling code here:
        Pomocna.idPozicioniranje = Integer.parseInt(tablica.getValueAt(tablica.getSelectedRow(), 0).toString());
        dispose();
    }//GEN-LAST:event_jButtonOdabirActionPerformed

    private void tablicaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablicaKeyPressed
        if (odabir && evt.getKeyCode() == KeyEvent.VK_ENTER) {
            evt.consume();
            jButtonOdabir.doClick();
        }
    }//GEN-LAST:event_tablicaKeyPressed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBrisanje;
    private javax.swing.JButton jButtonIzlaz;
    private javax.swing.JButton jButtonIzmjena;
    private javax.swing.JButton jButtonOdabir;
    private javax.swing.JButton jButtonUpis;
    private javax.swing.JPanel jPanel1;
    private quick.dbtable.DBTable tablica;
    // End of variables declaration//GEN-END:variables
}
